package com.view.pages.worldtown.objects
{
	import com.view.pages.worldtown.rooms.*;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.Collada;
	import org.papervision3d.objects.primitives.Plane;

	public class Shop extends DisplayObject3D
	{
		private var $loadedXml:Object;
		private var $loadedIn:Object;
		public var $shopIn:*;
		private var $dims:Array;
		private var $doorsImg:Bitmap;
		private var $objects:Array;
		public var $tx:Number;
		public var $tz:Number;
		public var index:int;
		private var $position:String;
		
		public var isIn:Boolean;
		
		private var $settings:Object;
		private var $loadedImages:Object;
		
		public var $shopOut:Collada;
		
		private var $dist:Number = 800;
		
		public static const DOORS_CLICKED:String = "doors_clicked";
		public static const DOORS_OPENED:String = "doors_opened";
		public static const DOORS_CLOSED:String = "doors_closed";
		
		private var $dW:Number;
		private var $dH:Number;
		
		private var $dynamicDoors:Doors3D;
		private var $staticDoors:Plane;
		private var $dynamicDoorsMat:MovieMaterial;
		public var $doorsMC:MovieClip;
		public var $floorHolder:DisplayObject3D;
		
		private var $r0:Room0;
		private var $r1:Room1;
		private var $r2:Room2;
		private var $r3:Room3;
		private var $r4:Room4;
		private var $r5:Room5;
		private var $r6:Room6;
		
		public function Shop(_loadedXml:Object,_loadedIn:Object,_doors:Bitmap, _dims:Array,_settings:Object,_loadedImages:Object)
		{
			//$loadedObject = _loadedObject;
			$loadedXml = _loadedXml;
			$loadedIn = _loadedIn;
			$dims = _dims;
			$doorsImg = _doors;
			$settings = _settings;
			$loadedImages = _loadedImages;
			super();
			//init();
		}
		public function get room():*
		{
			return $shopIn;
		}
		public function start():void
		{
			isIn = false;
			$dW = $loadedIn.doorsW;
			$dH = $loadedIn.doorsH;
			
			//ROOM
			var $cn:String = "com.view.pages.worldtown.rooms." + $dims[4];
			var $$Shop:Class = getDefinitionByName($cn) as Class;
			
			$shopIn = new $$Shop($loadedXml,$loadedIn,$loadedImages);
			//$shopIn.y += $loadedIn.h/2;
			$shopIn.rotationY = 90;
			//$shopIn.x += $loadedIn.d/2 + 40;
				
			//SHOP OUT
			$shopOut = new Collada($loadedXml.shop);
			addChild($shopOut);

			//DOORS MC
			$dynamicDoors = new Doors3D(DoorsMC,$loadedImages.doors,$dW,$dH);
			$dynamicDoors.addEventListener(Doors3D.DOORS_CLICKED,onClick);
			$dynamicDoors.addEventListener(Doors3D.DOORS_OPENED,onOpen);
			$dynamicDoors.addEventListener(Doors3D.DOORS_CLOSED,onClose);
			$dynamicDoors.addDoorsListener();
			$dynamicDoors.start();
			$dynamicDoors.y = $dH/2;
			$dynamicDoors.rotationY = 90;
			$dynamicDoors.z = 30;
			addChild($dynamicDoors);

			//MEASUREMENT OF WHOLE SHOP
			this.x = $dims[0];
			this.y = $dims[1];
			this.z = $dims[2];
			this.rotationY = $dims[3];
				
			if($dims[3] == 0)
			{
				$position = "right";
				$tx = $dims[0] - $dist;
				$tz = $dims[2];
			}
			else if($dims[3] == 180)
			{
				$position = "left";
				$tx = $dims[0] + $dist;
				$tz = $dims[2];
			}
			if($dims[3] == 90)
			{
				$position = "back";
				$tx = $dims[0];
				$tz = $dims[2] + $dist;
			}
		}
		private function onClick(_e:Event):void
		{
			dispatchEvent(new Event(DOORS_CLICKED));
			addChild($shopIn);
			$dynamicDoors.open();
			$shopOut.useOwnContainer = true;
			$shopIn.useOwnContainer = true;	
			$dynamicDoors.useOwnContainer = true;
		}
		private function onOpen(_e:Event):void
		{
			$dynamicDoors.close();	
			dispatchEvent(new Event(DOORS_OPENED));
		}
		private function onClose(_e:Event):void
		{
			
			//$shopOut.useOwnContainer = false;
			//$shopIn.useOwnContainer = false;
			//$dynamicDoors.useOwnContainer = false;
			
			//check if you are in the room
			trace("cam:"+$settings.camera+" ; this:"+this);
			if($position == "left")
			{
				if($settings.camera.x<this.x)
				{
					personCheck(true);
				}	
				else
				{
					personCheck(false);
				}
			}
			if($position == "right")
			{
				if($settings.camera.x>this.x)
				{
					personCheck(true);
				}	
				else
				{
					personCheck(false);
				}
			}
			if($position == "back")
			{
				if($settings.camera.z<this.z)
				{
					personCheck(true);
				}	
				else
				{
					personCheck(false);
				}
			}
			dispatchEvent(new Event(DOORS_CLOSED));
		}
		private function personCheck(_in:Boolean):void
		{
			if(!_in)
			{
				isIn = false;
				removeChild($shopIn);
			}
			else
			{
				isIn = true;
			}
		}
		public function hide():void
		{
			removeChild($shopIn);
			$dynamicDoors.remove();
		}
		public function overlapFix(_useCon:Boolean):void
		{
			$shopOut.useOwnContainer = _useCon;
			$shopIn.useOwnContainer = _useCon;
			$dynamicDoors.useOwnContainer = _useCon;
		}
	}
}